package com.lagou.edu.utils;

/**
 * 类型转换
 */
public class TypeUtil {

    public static Object stringToTarget(String str, Class<?> aClass) throws Exception {
        if (str != null && str.length() > 0) {
            if (double.class.equals(aClass)) {
                return Double.parseDouble(str);
            } else if (long.class.equals(aClass)) {
                return Long.parseLong(str);
            } else if (int.class.equals(aClass)) {
                return Integer.parseInt(str);
            } else if (float.class.equals(aClass)) {
                return Float.parseFloat(str);
            } else if (short.class.equals(aClass)) {
                return Short.parseShort(str);
            } else if (boolean.class.equals(aClass)) {
                return Boolean.parseBoolean(str);
            } else {
                return aClass.getConstructor(String.class).newInstance(str);
            }
        }
        return str;
    }
}
